// pages/index/my/my.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    isUser: false
  },
  //发布列表
  toReleaseList(){
    wx.navigateTo({
      url: './releaseList/releaseList'
    })
  },
  //历史计算
  toCalcHistory() {
    wx.navigateTo({
      url: './calcHistory/calcHistory'
    })
  },
  //联系我们
  contactUs() {
    wx.navigateTo({
      url: './contactUs/contactUs'
    })
  },
  //登录
  myLogin(userInfo) {
    let that = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          var url = app.globalData.api + "/wechat/user/login?code=" + res.code;
          var data = {
            code: res.code,
            userName: userInfo.nickName,
            headPortrait: userInfo.avatarUrl
          }
          wx.request({
            url: url,
            data: data,
            method: 'post',
            success: function (res2) {
              console.info(res2);
              if (res2.data.code == 0) {
                //设置全局用户信息
                app.globalData.userInfo = userInfo;
                app.globalData.isUser = true
                //设置openId缓存
                wx.setStorageSync('openId', res2.data.openid);
                wx.setStorageSync('session_key', res2.data.session_key);
                if (res2.data.token) {
                  wx.setStorageSync('token', res2.data.token);
                }
                //本页用户信息
                that.setData({
                  userInfo: userInfo,
                  isUser: true
                })
              } else {
                wx.showToast({
                  title: res2.data.errMsg,
                  icon: "none"
                })
              }

            },
          })
        } else {
          wx.showToast({
            title: "网络错误，请稍后重试~",
            icon: "none"
          })
        }
      }
    })
  },
  //调出重新授权界面
  regetInfo() {
    var that = this;
    wx.openSetting({
      success: function success(res) {
        console.log('进入了用户信息重复授权');
        console.info(res.authSetting);
        if (res.authSetting["scope.userInfo"] == true) {
          wx.getUserInfo({
            success: function (data2) {
              console.info("重复授权===", data2.userInfo);
              //登录
              that.myLogin(data2.userInfo);
            }
          })
        } else {
          console.info("用户进入了重复授权设置，但是并未授权")
          that.myWarn();
        }
      }
    });
  },
  //提示弹层
  myWarn() {
    var that = this;
    wx.showModal({
      title: '用户未授权',
      content: '如需获取用户联系方式，请按确定并在授权管理中选中“用户信息”，进行授权，最后再重新进入小程序即可正常使用。',
      showCancel: false,
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.regetInfo();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //登录
  getUserInfo: function (e) {
    var that = this;
    if (e.detail && e.detail.userInfo) {
      that.myLogin(e.detail.userInfo);
    } else{
      that.myWarn();
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var openId = (wx.getStorageSync('openId'));
    var that = this;
    console.info(openId);
    if (openId){
      //授权过
      console.info(app.globalData);
      wx.getUserInfo({
        success: function (data2) {
          console.info("有openID，要获取用户信息===", data2.userInfo);
          //本页用户信息
          that.setData({
            userInfo: data2.userInfo,
            isUser: true
          })
        }
      })
    }else{
      //未授权，需用户信息
      this.setData({
        isUser: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})